﻿namespace SolidMvvm {
	using System.Collections.ObjectModel;
	using System.Collections.Specialized;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Media;
	using System.Windows.Shapes;

	using SolidMvvmInterfaces;

	/// <summary>
	///     Interaction logic for WindowDeluxe.xaml
	/// </summary>
	public partial class WindowDeluxe {
		/// <summary>Client-area content such as a view model or UIElement. This defaults to null.</summary>
		public static readonly DependencyProperty ClientContentProperty = DependencyProperty.Register("ClientContent", typeof(object), typeof(WindowDeluxe), new FrameworkPropertyMetadata(null));

		public WindowDeluxe() {
			InitializeComponent();

			LeftStatusBarItemSnapInPoints = new ObservableCollectionEx<ISnapInPoint>();
			CenterStatusBarItemSnapInPoints = new ObservableCollectionEx<ISnapInPoint>();
			RightStatusBarItemSnapInPoints = new ObservableCollectionEx<ISnapInPoint>();
			LeftStatusBarItemSnapInPoints.CollectionChanged += StatusBarItemSnapInPoints_CollectionChanged;
			LeftStatusBarItemSnapInPoints.ItemChanged += StatusBarItemSnapInPoints_ItemChanged;
			CenterStatusBarItemSnapInPoints.CollectionChanged += StatusBarItemSnapInPoints_CollectionChanged;
			CenterStatusBarItemSnapInPoints.ItemChanged += StatusBarItemSnapInPoints_ItemChanged;
			RightStatusBarItemSnapInPoints.CollectionChanged += StatusBarItemSnapInPoints_CollectionChanged;
			RightStatusBarItemSnapInPoints.ItemChanged += StatusBarItemSnapInPoints_ItemChanged;
		}

		public ObservableCollectionEx<ISnapInPoint> LeftStatusBarItemSnapInPoints { get; }

		public ObservableCollectionEx<ISnapInPoint> CenterStatusBarItemSnapInPoints { get; }

		public ObservableCollectionEx<ISnapInPoint> RightStatusBarItemSnapInPoints { get; }

		/// <summary>Gets or sets the value of the <see cref="ClientContentProperty" />. This is a dependency property.</summary>
		public object ClientContent
		{
			get
			{
				return GetValue(ClientContentProperty);
			}
			set
			{
				SetValue(ClientContentProperty, value);
			}
		}

		void StatusBarItemSnapInPoints_ItemChanged(object sender, int index) {
			UpdateStatusBar();
		}

		void AddStatusBarItems(ItemsControl itemsControl, ObservableCollection<ISnapInPoint> snapInPoints, HorizontalAlignment horizontalAlignment) {
			for (var index = 0; index < snapInPoints.Count; index++) {
				itemsControl.Items.Add(snapInPoints[index].Content);
				//snapInPoints[index].PropertyChanged += this.WindowEx_PropertyChanged;
				if (index < snapInPoints.Count - 1)
					itemsControl.Items.Add(new Rectangle { Width = 10, Height = 10, HorizontalAlignment = horizontalAlignment, VerticalAlignment = VerticalAlignment.Stretch, /*Orientation = Orientation.Horizontal, */Fill = Brushes.Blue });
			}
		}

		void UpdateStatusBar() {
			itemsControlLeft.Items.Clear();
			itemsControlCenter.Items.Clear();
			itemsControlRight.Items.Clear();

			AddStatusBarItems(itemsControlLeft, LeftStatusBarItemSnapInPoints, HorizontalAlignment.Left);
			AddStatusBarItems(itemsControlCenter, CenterStatusBarItemSnapInPoints, HorizontalAlignment.Center);
			AddStatusBarItems(itemsControlRight, RightStatusBarItemSnapInPoints, HorizontalAlignment.Right);
		}

		void StatusBarItemSnapInPoints_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
			UpdateStatusBar();
		}
	}
}
